<template>
  <view class="viewport">
    <!-- 头像 -->
    <view class="avatar">
      <view class="avatar-content">
        <image class="image" :src="userForm.avatar" mode="aspectFill" />
        <text class="text" @tap="onAvatarChange">点击修改头像</text>
      </view>
    </view>
    <!-- 表单 -->
    <view class="form">
      <!-- 表单内容 -->
      <view class="form-content">
        <view class="form-item">
          <text class="label">账号</text>
          <text class="account">{{ userForm.account }}</text>
        </view>
        <view class="form-item">
          <text class="label">昵称</text>
          <input class="input" type="text" placeholder="请填写昵称" v-model="userForm.username" />
        </view>
        <view class="form-item">
          <text class="label">性别</text>
          <radio-group @change="ongenderChange">
            <label class="radio">
              <radio value="0" color="#27ba9b" :checked="userForm.sex === '0'" />
              男
            </label>
            <label class="radio">
              <radio value="1" color="#27ba9b" :checked="userForm.sex === '1'" />
              女
            </label>
          </radio-group>
        </view>
        <view class="form-item">
          <text class="label">城市</text>
          <view class="picker" @tap="openPicker">
            <input
              :placeholder="userForm.address ? '' : '请选择省市区'"
              disabled
              type="text"
              placeholder-class="input-placeholder"
              v-model="userForm.address"
            />
          </view>
        </view>
        <view class="form-item">
          <text class="label">简介</text>
          <input
            v-model="userForm.userInfo"
            class="input"
            type="text"
            placeholder-class="input-placeholder"
            :placeholder="userForm.userInfo ? '' : '请输入个人简介'"
          />
        </view>
      </view>
      <!-- 提交按钮 -->
      <button class="form-button" @tap="submit">保 存</button>
    </view>
    <!-- 省市区选择 province city area初始省市区设置 show:是否显示  @sureSelectArea：确认事件 @hideShow：隐藏事件-->
    <cc-selectDity
      :province="addressForm.province"
      :city="addressForm.city"
      :area="addressForm.area"
      :show="show"
      @changeClick="changeClick"
      @sureSelectArea="onsetCity"
      @hideShow="onhideShow"
    ></cc-selectDity>
  </view>
</template>


<script setup lang="ts">
import { useMemberStore } from '@/stores/index'
import { ref } from 'vue'
import type { userItem } from '@/types/userform'
import { baseURL } from '@/utils/http'
import { onLoad, onShow } from '@dcloudio/uni-app'
import { modifyUserInfo } from '@/services/center'
const memberStore = useMemberStore()
const userForm = ref<userItem>()
userForm.value = Object.assign({}, memberStore.profile!.user)
//修改头像
const onAvatarChange = () => {
  uni.chooseImage({
    success(res) {
      uni.uploadFile({
        url: baseURL + '/upload',
        filePath: res.tempFilePaths[0],
        name: 'image',
        success: (uploadFileRes: any) => {
          let path = JSON.parse(uploadFileRes.data)
          userForm.value!.avatar = path.data
        },
      })
    },
  })
}
const ongenderChange = (ev: any) => {
  userForm.value!.sex = ev.detail.value
}
const addressForm = ref({
  province: '北京市',
  city: '北京市',
  area: '东城区',
})
const show = ref(false)
const openPicker = () => {
  show.value = true
}
const changeClick = (value: string, value2: string, value3: string) => {
  addressForm.value.province = value
  addressForm.value.city = value2
  addressForm.value.area = value3
}
const onhideShow = () => {
  show.value = false
}
const onsetCity = (e: any) => {
  let data = e.detail.target.dataset
  let address = data.province + ' ' + data.city + ' ' + data.area
  userForm.value!.address = address
  show.value = false
}
const submit = async () => {
  if (userForm.value?.username == '' || userForm.value!.username.length > 20) {
    uni.showToast({ icon: 'none', title: '昵称不能为空,长度最长为20' })
    return
  }
  console.log(userForm.value)

  const res = await modifyUserInfo(userForm.value!)
  if (res.code == 200) {
    memberStore.setUserinfo(res.data)
    uni.showToast({ icon: 'none', title: '修改成功' })
    setTimeout(() => {
      uni.navigateBack()
    }, 500)
  }
}
</script>

<style lang="scss">
page {
  background-color: #f4f4f4;
}

.viewport {
  display: flex;
  flex-direction: column;
  height: 100%;
}
// 导航栏
.navbar {
  position: relative;
  .title {
    height: 40px;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 16px;
    font-weight: 500;
    color: #fff;
  }

  .back {
    position: absolute;
    height: 40px;
    width: 40px;
    left: 0;
    font-size: 20px;
    color: #fff;
    display: flex;
    justify-content: center;
    align-items: center;
  }
}

// 头像
.avatar::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 300rpx;
  background-image: url(https://pr1-greenteacdn.lenovo.com.cn/config/1672128837481_16-缩略.png);
  background-size: 100% 100%;
  filter: blur(4px);
  z-index: -1;
  background-size: cover;
}
.avatar {
  text-align: center;
  width: 100%;
  height: 300rpx;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;

  background-repeat: no-repeat;
  .image {
    width: 160rpx;
    height: 160rpx;
    border-radius: 50%;
    background-color: #eee;
  }

  .text {
    display: block;
    padding-top: 20rpx;
    line-height: 1;
    font-size: 26rpx;
    color: #fff;
  }
}

// 表单
.form {
  background-color: #f4f4f4;

  &-content {
    margin: 20rpx 20rpx 0;
    padding: 0 20rpx;
    border-radius: 10rpx;
    background-color: #fff;
  }

  &-item {
    display: flex;
    height: 96rpx;
    line-height: 46rpx;
    padding: 25rpx 10rpx;
    background-color: #fff;
    font-size: 28rpx;
    border-bottom: 1rpx solid #ddd;

    &:last-child {
      border: none;
    }

    .label {
      width: 180rpx;
      color: #333;
    }

    .account {
      color: #666;
    }

    .input {
      flex: 1;
      display: block;
      height: 46rpx;
    }

    .radio {
      margin-right: 20rpx;
    }

    .picker {
      flex: 1;
    }
    .input-placeholder {
      color: #6b6868;
    }
  }

  &-button {
    height: 80rpx;
    text-align: center;
    line-height: 80rpx;
    margin: 30rpx 20rpx;
    color: #fff;
    border-radius: 80rpx;
    font-size: 30rpx;
    background-color: #f04142;
  }
}
</style>